<template>
	<view class="index">
		<view class="content">
			<image src="https://foster-use.oss-cn-chengdu.aliyuncs.com/icon/login.png" mode=""></image>
			<view class="text">
				欢迎登录十里铺村QQ农场
			</view>
			<view class="btn"  @click="login('operator')">
				我是操作人
			</view>
			<view class="btn"  @click="login('user')">
				我是用户
			</view>
			
		</view>
		<!-- <uni-tabbar /> -->
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {weiXinLogin} from "@/api/api.js"
	export default {
		data() {
			return {
				userInfo:{},
				hasUserInfo:false,
				hasLogin:false,
				appSecret:'d753a568afb7a0e9429f7a64d88c67c5',
				appId:'wx13df9de78655e873',
				JsCode:'',
				grantType:'authorization_code',
			}
		},
		onLoad() {
			// this.getUserInfo()
			// this.checkOauth()
		},
		methods: {
			...mapMutations(['setUserInfo', 'setTabIndex']),
			checkOauth(){
				uni.getSetting({
					success: (res) => {
						console.log(res);
						if(!res.authSetting['scope.userInfo']){
							uni.authorize({
								scope:"scope.userInfo"
							})
						}else{
							console.log('用户已授权');
						}
					}
				})
			},
			getProvider(){
				uni.getProvider({
					service:'oauth',
					success: (res) => {
						console.log(res);
						this.provider = res.provider[0]
					},
					fail: (err) => {
						console.log(err);
					}
				})
			},
			weiXinLogin(){
				var _this = this
				uni.login({
					provider:"weixin",
					onlyAuthorize:true,
					success: (res) => {
						console.log('登录凭证：',res);
						this.JsCode = res.code
						const loginResult = weiXinLogin(this.JsCode)
						loginResult.then(result=>{
							console.log('微信登录：',result);
							if(result?.data.code == 200){
								let acccountData = result.data.data
								uni.setStorageSync("account",acccountData)
								this.hasLogin = true
								uni.hideLoading()
								wx.showModal({
									title:'提示',
									content:'获取你的昵称、头像、地区及性别',
									success: (res) => {
										if(res.confirm){
											uni.showLoading({
												mask:true
											})
											wx.getUserProfile({
											  desc: '用于完善会员资料',
											  success: res => {
											    console.log(res);
												_this.userInfo = res.userInfo
												uni.setStorageSync("weixinAccount",_this.userInfo)
												uni.hideLoading()
												// _this.hasUserInfo = true
												// console.log(_this.hasUserInfo);
												// uni.hideLoading()
												uni.switchTab({
													url: '/pages/index/index'
												})
											  },
											  fail: res => {
											    console.log(res)
											    //拒绝授权
												// uni.hideLoading()
											    wx.showToast({
											      title: '您拒绝了请求,不能正常使用小程序',
											      icon: 'error',
											      duration: 2000
											    });
												uni.hideLoading()
											    return;
											  }
											});
										}else if(res.cancel){
											console.log('点击了取消授权');
										}
									},
									
								})
							}else{
								uni.showToast({
									title:'登录失败',
									icon:'error'
								})
								uni.hideLoading()
							}
						}).catch(err=>{
							console.log(err);
							if(err){
								this.hasLogin = false
								uni.showToast({
									title:"服务器连接错误",
									icon:'error'
								})
								uni.hideLoading()
							}
						})
					}
				})
			},
			hasStorageInfo(){
				uni.login({
					provider:"weixin",
					onlyAuthorize:true,
					success: (res) => {
						this.JsCode = res.code
						const result = weiXinLogin(this.JsCode).then((loginData)=>{
							if(loginData?.data.code == 200){
								let acccountData = loginData.data.data
								uni.setStorageSync("account",acccountData)
								this.hasLogin = true
								uni.switchTab({
									url: '/pages/index/index'
								})
							}else{
								uni.showToast({
									icon:'error',
									title:"登录失败",
									mask:true
								})
							}
							uni.hideLoading()
						}).catch((err)=>{
							uni.showToast({
								icon:'error',
								title:"服务器连接错误",
								mask:true
							})
							uni.hideLoading()
						})
					}
				})
			},
			getUserInfo(e) {
			    var _this = this
				
				//如果本地有登录缓存
				if(uni.getStorageSync("account").token){
					uni.showLoading({
						mask:true,
						title:"登录中"
					})
					this.hasStorageInfo()
				}else{
					wx.showModal({
					  title: '温馨提示',
					  content: '亲，授权微信登录后才能正常使用小程序功能',
					  success(res) {
					    console.log(res)
					    //如果用户点击了确定按钮
					    if (res.confirm) {
							uni.showLoading({
								mask:true
							})
							_this.getProvider()
							_this.weiXinLogin()
					    } else if (res.cancel) {
					      //如果用户点击了取消按钮
					      wx.showToast({
					        title: '您拒绝了请求,不能正常使用小程序',
					        icon: 'error',
					        duration: 2000
					      });
					      return;
					    }
					  },
					});
				}
			},
			login(i) {
				this.setTabIndex(0)
				this.setUserInfo(i)
				// #ifdef MP-WEIXIN
				if (i == 'operator') {
					uni.navigateTo({
						url: '/pages/login/login'
					})
				} else{
					this.getUserInfo()
				}
				// #endif
				// #ifdef H5
				if (i == 'operator') {
					uni.switchTab({
						url: '/pages/operator/operator'
					})
				} else {
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
				// #endif
			},
		}
	}
</script>

<style>
	body {
		background-color: #F4F6F9;
	}
</style>
<style lang="scss" scoped>
	.index {
		// width: 750rpx;
		// height: 100%;
		background: linear-gradient(194deg, #AFFFE6 0%, #FFFFFF 36%, #FFFFFF 100%);
		position: fixed;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;

		.df {
			display: flex;
		}

		.content {
			width: 630rpx;
			padding: 0 60rpx 0 60rpx;
			position: absolute;
			top: 292rpx;
			text-align: center;

			image {
				width: 178rpx;
				height: 178rpx;
				border-radius: 60rpx;
				box-shadow: 0 0 10px #b0b0b0;
				margin: auto;
			}

			.text {
				margin-top: 30rpx;
				margin-bottom: 235rpx;
				font-size: 38rpx;
				font-weight: bold;
				letter-spacing: 3rpx;
			}

			.btn {

				width: 630rpx;
				height: 96rpx;
				background: #17CC77;
				border-radius: 44rpx;
				text-align: center;
				font-size: 34rpx;
				color: #FFFFFF;
				line-height: 96rpx;
				margin-bottom: 96rpx;
			}
		}
	}
</style>